<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/22
 * Time: 15:44
 */
namespace app\admin\controller;
use think\Db;
class Cash extends Base {

    public function index(){
        if(request()->isAjax()){
            $params = input('param.');
            $where = array();
            if(isset($params['apply_status']) && $params['apply_status'] != '')
            {
                $where['a.apply_status'] = $params['apply_status'];
            }
            if(isset($params['apply_type']) && $params['apply_type'] != '')
            {
                $where['a.apply_type'] = $params['apply_type'];
            }
            if(isset($params['key']) && $params['key'] != '')
            {
                $where['uuu.user_username|uuu.user_phone'] = ['like', "%" . $params['key'] . "%"];
            }
//            $where['a.task_status'] = array('NEQ',5);
//            $where['a.apply_status'] = array('NEQ',3);
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('apply')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.apply_user_id','left')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('apply')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.apply_user_id','left')
                ->where($where)
                ->page($Nowpage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone')
                ->order('a.apply_status desc,applt_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    //打款
    public function money(){
        $ids = input('id');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择未打款数据']);
        }
        $status = Db::table('up_apply')->where('apply_id', 'IN', $ids)->column('apply_status');
        if(in_array(1, $status) || in_array(2, $status)){
            return json(['code' => 100, 'msg' => '所选数据中有已打款或已拒绝，请重新选择']);
        }
        if(request()->isPost()){
            $id = input('param.id');
            $data['apply_content'] = input('param.apply_content');
            $data['apply_status'] = input('param.status');
            if(!input('param.status')){
                return json(['code' => 100, 'msg' => "填写金额和备注"]);
            }
            $res = Db::table('up_apply')->where('apply_id', 'IN', $id)->update($data);
//            $info = Db::table('up_apply')->where('apply_id', 'IN', $id)->find();
//            allInDetail($info['apply_user_id'],2,'提现',$info['apply_money'],7);
            if($res){
                if($data['apply_status'] == 2){
                    $ss = Db::table('up_apply')->where('apply_id', 'IN', $ids)
                        ->field('apply_user_id,apply_money,apply_active')->select();
                    foreach ($ss as $k=>$v){
                        if($v['apply_active'] == 1){
                            Db::table('up_u_user')->where('user_id', $v['apply_user_id'])
                                ->setInc('user_all_money',$v['apply_money']*10);
                        }else{
                            Db::table('up_u_user')->where('user_id', $v['apply_user_id'])
                                ->setInc('user_active_money',$v['apply_money']*10);
                        }

                    }
                }
                return json(['code' => 200, 'msg' => "操作成功"]);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }

        $this->assign('id',$ids);
        return $this->fetch();
    }

    //单页导出
    public function excelIn(){
        $ids = input('param.id');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择数据']);
        }
        $where['a.apply_id'] = $ids;
        $lists = Db::name('apply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.apply_user_id','left')
            ->where($where)
            ->field('a.*,uuu.user_username,uuu.user_phone')
            ->find();
        if($lists['apply_type']==1){
            $lists['apply_type_name'] = '支付宝支付';
        }elseif ($lists['apply_type']==2){
            $lists['apply_type_name'] = '微信支付';
        }
        if($lists['apply_status']==1){
            $lists['apply_status_name'] = '已打款';
        }elseif ($lists['apply_status']==3){
            $lists['apply_status_name'] = '未打款';
        }else{
            $lists['apply_status_name'] = '拒绝';
        }
        if($lists['apply_active']==1){
            $lists['apply_active_name'] = '普通任务提现';
        }elseif ($lists['apply_active']==2){
            $lists['apply_active_name'] = '活动任务提现';
        }
        $res = ExcelCash('提现管理列表','提现管理列表',$lists);
        return json($res);
    }
    /**
     * 设置提现手续费率
     * @return mixed|\think\response\Json
     */
    public function feilv(){
        if(request()->isPost()){
            $price = input('price');
            if(!is_numeric($price)){
                return json(['code' => 100, 'msg' => '请输入数字']);
            }
            if($price < 0 || $price >= 1000){
                return json(['code' => 100, 'msg' => '费率在0到1000之间']);
            }
            $res = Db::table('up_about_us')->where('about_id', 1)->update(['cash_feilv' => $price]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $price = Db::table('up_about_us')->where('about_id', 1)->value('cash_feilv');
        $this->assign('price', $price);
        return $this->fetch('cash/feilv');
    }


    //----------------------------代理商的充值明细---------------------------------------------------------------------
    public function daili(){
        if(request()->isAjax ()){
            extract(input());
            $where2 = [];
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $where2['dlmx_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $where2['dlmx_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $where2['dlmx_time'] = ['between time',[$start,$end]];
            }
            if(isset($daili)&&$daili!="")
            {
                $where2['dlmx_dl_id'] = $daili;
            }
            $where2['dlmx_id'] = ['>',0];
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_dlmx')->alias('l')
                ->join('up_dl d', 'l.dlmx_dl_id=d.dl_id','left')
                ->where($where2)
                ->count();// 获取总条数
            $lists = Db::table('up_dlmx')->alias('l')
                ->join('up_dl d', 'l.dlmx_dl_id=d.dl_id','left')
                ->where($where2)
                ->order('dlmx_time desc')
                ->page($Nowpage,$limits)
                ->field('l.*,d.dl_name,dl_balance')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user = Db::table('up_dl')->where('dl_status',2)->field('dl_id,dl_name')->select();
        $this->assign('user',$user);
        return $this->fetch('cash/daili');
    }
}